package com.bjesc_b2c.app.appraiset.widget.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.widget.BaseDialog;
import com.example.codeutils.utils.ToastUtils;

/**
 * Created by yidong on 2017/7/28.
 */

public class AddCarDialog  extends BaseDialog implements View.OnClickListener {
    private int changeCase;
    private String srString;
    private CallBack callBack;
    private EditText vinEd;
    public AddCarDialog(Context context, CallBack callBack) {
        super(context);
        this.callBack = callBack;
        initViews();
    }

    private void initViews() {
        view = inflater.inflate(R.layout.dialog_add_car, null);
        view.findViewById(R.id.tv_cancel).setOnClickListener(this);
        view.findViewById(R.id.tv_sure).setOnClickListener(this);
        vinEd = (EditText)view.findViewById(R.id.ed_vin_num);
        //给需要的文本框添加TextChangedListener方法
        vinEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 将输入的小写字母实时转换成大写
                if (changeCase == 0) {
                    srString = s.toString().toUpperCase();
                    if (s.toString().equals("")) {
                        changeCase = 0;
                    } else {
                        changeCase = 1;
                    }
                    s.clear();
                } else if (changeCase == 1) {
                    changeCase = 2;
                    s.append(srString);
                } else {
                    changeCase = 0;
                }
            }
        });
        setContentView(view);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_sure:
                if (callBack != null ) {
                    if(!TextUtils.isEmpty(vinEd.getText().toString()) &&
                            vinEd.getText().toString().length() == 17 ){
                        callBack.callBack(vinEd.getText().toString());
                        dismiss();
                    }else{
                        ToastUtils.showLongToast("VIN码不正确");
                    }

                }
                break;
        }
    }

    public interface CallBack {
        void callBack(String vin);
    }
}